
export default class ByteStream {

    constructor(data) {

        this.data = data;
        this.pos = 0;
    }

    readByte() {
        return this.data[this.pos++];
    }

    peekByte() {
        return this.data[this.pos];
    }

    readBytes(n) {
        return this.data.subarray(this.pos, this.pos += n);
    }

    peekBytes(n) {
        return this.data.subarray(this.pos, this.pos + n);
    }

    readString(len) {
        let str = '';
        for(let i=0; i<len; i++){
            str += String.fromCharCode(this.readByte());
        }
        return str;
    }

    readBitArray() {
        var arr = [];
        var bite = this.readByte();
        for (var i = 7; i >= 0; i--) {
            arr.push(!!(bite & (1 << i)));
        }
        return arr;
    }

    readUnsigned(littleEndian) {
        var a = this.readBytes(2);
        if(littleEndian){
            return (a[1] << 8) + a[0];	
        }else{
            return (a[0] << 8) + a[1];
        }	
    }
}